package my.jbf.firstGame;

import android.annotation.SuppressLint;
import android.app.*;
import android.database.Cursor;
import android.os.*;
import android.util.Log;
import android.view.*;
import android.content.pm.*;

import my.jbf.firstGame.GAMEDATA.GameDBManager;
import my.jbf.firstGame.GAMEDATA.SharedPreferenceManager;
import my.jbf.firstGame.GAMEDATA.UserDBManager;
import my.jbf.firstGame.SURFACEVIEW.FightSurfaceview;
import my.jbf.firstGame.SURFACEVIEW.LifeSurfaceView;
import my.jbf.firstGame.SURFACEVIEW.LoggingSurfaceView;

import android.widget.*;
import android.view.View.*;
import android.graphics.*;



//尽写了本地注册，以及本地检查是否已有用户，主要涉及sign()和sign_CheckUser()
public class MainActivity extends Activity implements OnClickListener
{
    public static final boolean ifdebug = true;
    private FightSurfaceview mFightSView;
    private LifeSurfaceView mLifeSView;
    private LoggingSurfaceView mLoggingSView;
    public static SharedPreferenceManager mSPM;
    private Button loginButton;
    private Button signButton;
    private EditText nameEditText;
    private EditText pswEditText;
    private CheckBox pswCheckBox;
    private CheckBox AutologinCheckBox;
    private RadioButton netRadioaButton;
    private RadioButton localRadioButton;
    private FrameLayout frameLayout;
    private Button settingButton;
	private EditText pswAgainEditText;
    private UserDBManager mUserDBManager;
    private GameDBManager mGameDBManager;
    private  LinearLayout mLinearLayout;
	private boolean isSign=false;


	

    @Override
    protected void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        //隐藏标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
							 WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mSPM = new SharedPreferenceManager(this);
		mUserDBManager = UserDBManager.getInstance(this);




        //mUserDBManager=UserDBManager.getInstance(this);
        logining();
		//测试专用！！！
		mSPM.init();
    }


    private void logining()
	{
        setContentView(R.layout.login);


        settingButton = findViewById(R.id.settingButton);
        frameLayout = findViewById(R.id.mainFrameLayout);
        loginButton = findViewById(R.id.loginButton);
        signButton = findViewById(R.id.signButton);
        nameEditText = findViewById(R.id.nameInPut);
        pswEditText = findViewById(R.id.PSWinPut);
        pswCheckBox = findViewById(R.id.pswCheckBox1);
        AutologinCheckBox = findViewById(R.id.autoLogin_CheckBox);
        netRadioaButton = findViewById(R.id.net_RadioButton);
        localRadioButton = findViewById(R.id.local_RadioButton);
        localRadioButton.setChecked(!mSPM.getNetRadioButton());
        netRadioaButton.setChecked(mSPM.getNetRadioButton());
		pswAgainEditText = findViewById(R.id.pswAgainEditText);
		mLinearLayout = findViewById(R.id.mainLinearLayout);
        loginButton.setOnClickListener(this);
        signButton.setOnClickListener(this);
        pswCheckBox.setOnClickListener(this);
        pswCheckBox.setChecked(mSPM.getSavePSWCheckButton());
        AutologinCheckBox.setOnClickListener(this);
        AutologinCheckBox.setChecked(mSPM.getAutoLoginCheckButton());
        netRadioaButton.setOnClickListener(this);
        localRadioButton.setOnClickListener(this);
        settingButton.setOnClickListener(this);
        //设置按钮透明
        signButton.setBackgroundColor(Color.TRANSPARENT);
        settingButton.setBackgroundColor(Color.TRANSPARENT);
        //loginButton.setBackgroundColor(Color.TRANSPARENT);
        mFightSView = findViewById(R.id.FightSurfaceVoew);
        nameEditText.setText(mSPM.getUserName());
        netRadioaButton.setClickable(false);
        if (mSPM.getSavePSWCheckButton())
        {
            pswEditText.setText(mSPM.getUserPSW());
        }
        if (mSPM.getAutoStartType())
        {
            mFightSView.setActivityShow();
            mLinearLayout.setVisibility(LinearLayout.GONE);
        }

		//以下代码测试用
		mFightSView.setActivityShow();
		mLinearLayout.setVisibility(LinearLayout.GONE);


    }

    private void fight()
	{
        mFightSView = new FightSurfaceview(this);
        setContentView(mFightSView);
    }

    @Override
    protected void onResume()
	{
        // TODO: Implement this method
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
		{
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        super.onResume();
    }

    private void startLoggingSurfaceView()
	{
        //mLoggingSView=new LoggingSurfaceView(this);
        mLifeSView = null;
        mFightSView = null;
    }

    private void startFightSurfaceView()
	{
        mLoggingSView = null;
        mLifeSView = null;
        mFightSView = new FightSurfaceview(this);
        setContentView(mFightSView);
    }

    private void startLifeSurfaceView()
	{
        mLoggingSView = null;
        //mLifeSView=new LifeSurfaceView(this);
        mFightSView = null;
    }

    private void GameInit()
	{

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View p1)
	{

        // TODO: Implement this method
        switch (p1.getId())
		{
            case R.id.autoLogin_CheckBox: {
					autoLogin_CheckBoxClick();
					break;
				}
            case R.id.pswCheckBox1: {
					pswCheckBoxClick();
					break;
				}
            case R.id.net_RadioButton: {
					netRadioButtonClick();
					break;
				}
            case R.id.local_RadioButton: {
					localRadioButtonClick();
					break;
				}
            case R.id.loginButton: {
					loginButtonClick();
					break;
				}
            case R.id.signButton: {
					signButtonClick();
					break;
				}
            case R.id.settingButton:
				{
					settingButtonClick();
				}
        }

    }

    private void settingButtonClick()
    {
        nameEditText.setText(String.valueOf(System.currentTimeMillis()));
        mSPM.resetSmallMap();
    }

    private void netRadioButtonClick()
	{
        // TODO: Implement this method
        if (!mSPM.getNetRadioButton())
		{
            mSPM.setNetRadioButton();
        }

    }

    private void localRadioButtonClick()
	{
        // TODO: Implement this method
        if (mSPM.getNetRadioButton())
		{
            mSPM.setNetRadioButton();
        }

    }

    private void loginButtonClick()
	{
        // TODO: Implement this method

		if (isSign)
		{//注册状态
		    sign();
		}
		else
		{//登录状态

			login();
		}




    }



    private void login()
	{//登录过程处理
		// TODO: Implement this method
		//设置自动登录
        switch (sign_CheckUser())
        {
            case 1:
				{//账号长度为6-50，且只能使用大小写字母及数字
					Toast.makeText(this, "账号长度为6-50，且只能使用大小写字母及数字", Toast.LENGTH_SHORT).show();
					break;
				}
            case 2:
				{//密码长度为6-50，且只能使用大小写字母及数字
					Toast.makeText(this, "密码长度为6-50，且只能使用大小写字母及数字", Toast.LENGTH_SHORT).show();
					break;
				}
            case 5:
				{//没有检测到用户名
					Toast.makeText(this, "用户名不存在，请注册或重新输入", Toast.LENGTH_SHORT).show();
					break;
				}
            case 6:
				{//密码错误
					pswEditText.setText("");
					Toast.makeText(this, "密码错误，请重新输入", Toast.LENGTH_SHORT).show();
					break;
				}
            case 0: {
					Toast.makeText(this, "可以登录" , Toast.LENGTH_SHORT).show();
					Log.d("jbf", "can be login");
					Cursor mCursor=mUserDBManager.getUserPSWandID(nameEditText.getText().toString().trim());
					mCursor.moveToNext();
					mSPM.setUserID(mCursor.getString(mCursor.getColumnIndex((getString(R.string.USER_PSW_PlayID)))));
					mCursor.close();
					if (AutologinCheckBox.isChecked())
					{
						if (!mSPM.getAutoStartType())
						{
							mSPM.setAutoStartType();
						}
					}
					if (pswCheckBox.isChecked())
					{
						mSPM.setUserPSW(pswEditText.getText().toString().trim());
					}
					mSPM.setUserName(nameEditText.getText().toString().trim());
					mFightSView.setActivityShow();
					mLinearLayout.setVisibility(LinearLayout.GONE);
				}
        }
	}

    private void signButtonClick()
	{//注册键事件
        // TODO: Implement this method


		isSign = !isSign;
		if (isSign)
		{
			signButton.setText("返回");
			loginButton.setText("注册");
			pswAgainEditText.setVisibility(View.VISIBLE);
		}
		else
		{
			signButton.setText("注册");
			loginButton.setText("登录");
			pswAgainEditText.setVisibility(View.INVISIBLE);
		}
    }

    private void pswCheckBoxClick()
	{
        // TODO: Implement this method
        if (mSPM.getSavePSWCheckButton() && mSPM.getAutoLoginCheckButton())
		{
            AutologinCheckBox.setChecked(false);
        }
        mSPM.setSavePSWCheckButton(pswCheckBox.isChecked());
        mSPM.setAutoLoginCheckButton(AutologinCheckBox.isChecked());
	}
    private void autoLogin_CheckBoxClick()
	{
        // TODO: Implement this method
        if (!mSPM.getSavePSWCheckButton() && !mSPM.getAutoLoginCheckButton())
		{
            pswCheckBox.setChecked(true);
        }
        mSPM.setSavePSWCheckButton(pswCheckBox.isChecked());
        mSPM.setAutoLoginCheckButton(AutologinCheckBox.isChecked());
    }
    private int sign_CheckUser()
	{
        String name = nameEditText.getText().toString().trim();
        String regex = "^[a-zA-Z0-9]+$";
        if (name.length() < 6 || !(name.matches(regex)) || name.length() > 50)
		{//账号长度为6-50，且只能使用大小写字母及数字
            return 1;
        }
        String PSW = pswEditText.getText().toString().trim();
        if (PSW.length() < 6 || !(PSW.matches(regex)) || PSW.length() > 50)
		{//密码长度为6-50，且只能使用大小写字母及数字
            return 2;
        }
        if (isSign && !(PSW.equals(pswAgainEditText.getText().toString())))
		{//两次输入的密码不一致
			return 3;
        }
        if (isSign)
		{
            if (mUserDBManager.isUserNameRepetition(name))
            {//注册状态时检测到有同样用户名
                return 4;
            }
        }
        else
        {
            if (!mUserDBManager.isUserNameRepetition(name))
            {//登录状态时没有检测到用户名
                return 5;
            }
            else
            {//登录状态时检测到有同样用户名
                Cursor mCursor=mUserDBManager.getUserPSWandID(name);
                mCursor.moveToNext();
                if (!PSW.equals(mCursor.getString(mCursor.getColumnIndex(getString(R.string.USER_PSW_Password)))))
                {//密码不同，不能登录
                    mCursor.close();
                    return 6;
                }
            }
        }

        return 0;
    }
	private void sign()
	{
		switch (sign_CheckUser())
		{
			case 1: 
				{//账号长度为6-50，且只能使用大小写字母及数字
					Toast.makeText(this, "账号长度为6-50，且只能使用大小写字母及数字", Toast.LENGTH_SHORT).show();
					break;
				}
			case 2: 
				{//密码长度为6-50，且只能使用大小写字母及数字
					Toast.makeText(this, "密码长度为6-50，且只能使用大小写字母及数字", Toast.LENGTH_SHORT).show();
					break;
				}
			case 3: 
				{//两次输入的密码不一致
				    pswEditText.setText("");
				    pswAgainEditText.setText("");
					Toast.makeText(this, "两次输入的密码不一致，请重新输入密码", Toast.LENGTH_SHORT).show();
					break;
				}
			case 4: 
				{//注册状态时检测到有同样用户名
					Toast.makeText(this, "用户名已被注册，请更换用户名", Toast.LENGTH_SHORT).show();
					break;
				}

			case 0: {
					Toast.makeText(this, "已在" + (mSPM.getNetRadioButton() ? "服务器" : "本地" + "注册账号，请牢记用户名"), Toast.LENGTH_SHORT).show();
					isSign = !isSign;
					signButton.setText("注册");
					loginButton.setText("登录");
					pswAgainEditText.setVisibility(View.INVISIBLE);
					pswAgainEditText.setText("");
					if (mSPM.getNetRadioButton())
					{

					}
					else
					{
						mUserDBManager.createUser(nameEditText.getText().toString().trim(), pswEditText.getText().toString().trim());
					}
				}
		}
	}



}
